import {exportCustomerList, getCustomerList, getReconciliationList, exporReconciliationList, getOrderBilldetailList, exporOrderBilldetailList, getDZD} from "../../services/report/customer";
import Constants from "../../utils/Constants";
import {message} from 'antd';
const R=require("ramda");
export default {
    namespace:"customerReport",
    state:{
        search:{
            lesseeCode:null,
        },
        page: {
            page: 1,
            size: 10,
            totalRow: 0,
            totalPage: 0
        },
        cstList:[],
        reconciliationList:[],
        reconciliationPage: {
            page: 1,
            size: 10,
            totalRow: 0,
            totalPage: 0
        },
        orderBilldetailList:[],
        orderBilldetailPage: {
            page: 1,
            size: 10,
            totalRow: 0,
            totalPage: 0
        },
    },
    reducers: {

        setCustomerList(state,{payload}) {
            return {...state,cstList:payload.customerList,page:payload.customerPage}
        },



        setReconciliationList(state, {payload}) {
            return {
                ...state,
                reconciliationList: payload.reconciliationList,
                reconciliationPage: payload.reconciliationPage
            }
        },

        setOrderBilldetailList(state, {payload}) {
            return {
                ...state,
                orderBilldetailList: payload.list,
                orderBilldetailPage: payload.page
            }
        }


    },
    effects:{
            *getCustomerList({payload},{call,put,select}){

               payload= R.isNil(payload)?null:payload;
               let page=R.isNil(payload)?Constants.DEFAULTPAGE:R.isNil(payload.page)?Constants.DEFAULTPAGE:payload.page;
               let beginTime=R.isNil(payload)?null:payload.beginTime;
               let endTime=R.isNil(payload)?null:payload.endTime;
               let status=R.isNil(payload)?null:payload.status;
               const res=yield call(getCustomerList,{beginTime:beginTime,endTime:endTime,status:status,page});
               let content=res.data.content;
               let customerList=R.isNil(content.data.list)?[]:content.data.list;
               if(res.data.resultCode=="ok"){
                 yield put({
                       type: 'setCustomerList',
                       payload: {
                           customerList: customerList,
                           customerPage:R.isNil(content.data.page) ? Constants.DEFAULTPAGE : content.data.page,
                        }
                        });
               }



            },


        /**
         * 查询客户对账单
         * @param payload
         * @param call
         * @param put
         * @param select
         * @returns {IterableIterator<*>}
         */
            *getReconciliationList({payload}, {call, put, select}) {

                payload = R.isNil(payload) ? null : payload;
                let page = R.isNil(payload) ? Constants.DEFAULTPAGE : R.isNil(payload.page) ? Constants.DEFAULTPAGE : payload.page;
                let beginTime = R.isNil(payload) ? null : payload.beginTime;
                let endTime = R.isNil(payload) ? null : payload.endTime;
                let cstId = R.isNil(payload) ? null : payload.cstId;
                let contractNo = R.isNil(payload) ? null : payload.contractNo;

                const res = yield call(getReconciliationList, {beginTime: beginTime, endTime: endTime, cstId: cstId, contractNo: contractNo, page});
                let content = res.data.content;
                let reconciliationList = R.isNil(content.data.list) ? [] : content.data.list;

                if (res.data.resultCode == "ok") {
                    yield put({
                        type: 'setReconciliationList',
                        payload: {
                            reconciliationList: reconciliationList,
                            reconciliationPage: R.isNil(content.data.page) ? Constants.DEFAULTPAGE : content.data.page,
                        }
                    });
                }

            },

        /**
         * 查询客户订单账目明细
         * @param payload
         * @param call
         * @param put
         * @param select
         * @returns {IterableIterator<*>}
         */
        *getOrderBilldetailList({payload}, {call, put, select}) {

            payload = R.isNil(payload) ? null : payload;
            let page = R.isNil(payload) ? Constants.DEFAULTPAGE : R.isNil(payload.page) ? Constants.DEFAULTPAGE : payload.page;
            let beginTime = R.isNil(payload) ? null : payload.beginTime;
            let endTime = R.isNil(payload) ? null : payload.endTime;
            let cstId = R.isNil(payload) ? null : payload.cstId;
            let contractNo = R.isNil(payload) ? null : payload.contractNo;

            const res = yield call(getOrderBilldetailList, {beginTime: beginTime, endTime: endTime, cstId: cstId, contractNo: contractNo, page});
            let content = res.data.content;
            let reconciliationList = R.isNil(content.data.list) ? [] : content.data.list;

            if (res.data.resultCode == "ok") {
                yield put({
                    type: 'setOrderBilldetailList',
                    payload: {
                        list: reconciliationList,
                        page: R.isNil(content.data.page) ? Constants.DEFAULTPAGE : content.data.page,
                    }
                });
            }

        },

        /**
         * 导出客户
         * @param payload
         * @param call
         * @param put
         * @param select
         * @returns {IterableIterator<*>}
         */
        *exportCustomerList({payload},{call,put,select}){
            payload= R.isNil(payload)?null:payload;
            let beginTime=R.isNil(payload)?null:payload.beginTime;
            let endTime=R.isNil(payload)?null:payload.endTime;
            let status=R.isNil(payload)?null:payload.status;
            const res=yield call(exportCustomerList,{beginTime:beginTime,endTime:endTime,status:status});

        },

        /**
         * 导出客户结算详情表
         * @param payload
         * @param call
         * @param put
         * @param select
         * @returns {IterableIterator<*>}
         */
        *exporReconciliationList({payload},{call,put,select}){
            const res=yield call(exporReconciliationList,{...payload});

        },

        /**
         * 导出客户对账单
         * @param payload
         * @param call
         * @param put
         * @param select
         * @returns {IterableIterator<*>}
         */
        *getDZD({payload},{call,put,select}){
            const res=yield call(getDZD,{...payload});
        },


        /**
         * 导出客户订单账目明细
         * @param payload
         * @param call
         * @param put
         * @param select
         * @returns {IterableIterator<*>}
         */
        *exporOrderBilldetailList({payload},{call,put,select}){
            const res=yield call(exporOrderBilldetailList,{...payload});

        }




    },

    subscriptions: {
        setup({history, dispatch}) {
            return history.listen(({pathname}) => {
                if(pathname.includes('report/customer')) {
                    dispatch({
                        type: 'getCustomerList'
                    });
                }
            });
        }
    }
}